var express = require('express');
var router = express.Router();
var path = require('path');
const { use } = require('./users');
var sqlite3 = require('sqlite3').verbose();

/* GET home page. */
router.get('/', function(req, res, next) {
  var db=new sqlite3.Database(path.join(__dirname, "../public/db/data.s3db"));
  sqlStr="SELECT * from no1"
  s1="select wt,answer from no1 where  wt=='r1'"
  s2="select wt,answer from no1 where  wt=='r2'"
  s3="select wt,answer from no1 where  wt=='r3'"
  s4="select wt,answer from no1 where  wt=='r4'"
  s5="select wt,answer from no1 where  wt=='r5'" 
  s6="select wt,answer from no1 where  wt=='r5' or wt=='r4' or wt=='r3' or wt=='r2' or wt=='r1'"
/*
  y1="select wt,answer from no1 where user in (select user from no1 where wt=='gn' and answer=='1') and wt=='r2'"
  y2="select wt,answer from no1 where user in (select user from no1 where wt=='gn' and answer=='2') and wt=='r2'"
  y3="select wt,answer from no1 where user in (select user from no1 where wt=='gn' and answer=='3') and wt=='r2'"
  y4="select wt,answer from no1 where user in (select user from no1 where wt=='gn' and answer=='4') and wt=='r2'"
  y5="select wt,answer from no1 where user in (select user from no1 where wt=='gn' and answer=='5') and wt=='r2'"

  w1="select wt,answer from no1 where user in (select user from no1 where wt=='jg' and answer=='1') and wt=='r3'"
  w2="select wt,answer from no1 where user in (select user from no1 where wt=='jg' and answer=='2') and wt=='r3'"
  w3="select wt,answer from no1 where user in (select user from no1 where wt=='jg' and answer=='3') and wt=='r3'"
  w4="select wt,answer from no1 where user in (select user from no1 where wt=='jg' and answer=='4') and wt=='r3'"
  w5="select wt,answer from no1 where user in (select user from no1 where wt=='jg' and answer=='5') and wt=='r3'"

  t1="select wt,answer from no1 where user in (select user from no1 where wt=='sp' and answer=='1') and wt=='r4'"
  t2="select wt,answer from no1 where user in (select user from no1 where wt=='sp' and answer=='2') and wt=='r4'"
  t3="select wt,answer from no1 where user in (select user from no1 where wt=='sp' and answer=='3') and wt=='r4'"
  t4="select wt,answer from no1 where user in (select user from no1 where wt=='sp' and answer=='4') and wt=='r4'"
  t5="select wt,answer from no1 where user in (select user from no1 where wt=='sp' and answer=='5') and wt=='r4'"

  u1="select wt,answer from no1 where user in (select user from no1 where wt=='xx' and answer=='1') and wt=='r5'"
  u2="select wt,answer from no1 where user in (select user from no1 where wt=='xx' and answer=='2') and wt=='r5'"
  u3="select wt,answer from no1 where user in (select user from no1 where wt=='xx' and answer=='3') and wt=='r5'"
  u4="select wt,answer from no1 where user in (select user from no1 where wt=='xx' and answer=='4') and wt=='r5'"
  u5="select wt,answer from no1 where user in (select user from no1 where wt=='xx' and answer=='5') and wt=='r5'"
  */
  var kk;
  db.all(s6,function(err,row){
    if (err) throw err
    else {
      if(row!=[])
        console.log(row)
        res.send(row)
    }
  });
  // db.all(s1,function(err,row){
  //   if (err) throw err
  //   else {
  //     if(row!=[])
  //       global.kk=row;
  //       console.log("第一次：",global.kk)
  //   }
  // });
  // db.all(s2,function(err,row){
  //   if (err) throw err
  //   else {
  //     if(row!=[])
  //     global.kk=global.kk.concat(row);
  //     console.log("第二次：",global.kk)
  //   }
  // });
  // db.all(s3,function(err,row){
  //   if (err) throw err
  //   else {
  //     if(row!=[])
  //     global.kk=global.kk.concat(row);
  //     console.log("第三次：",global.kk)
  //   }
  // });
  // db.all(s4,function(err,row){
  //   if (err) throw err
  //   else {
  //     if(row!=[])
  //     global.kk=global.kk.concat(row);
  //     console.log("第四次：",global.kk)
  //   }
  // });
  // db.all(s5,function(err,row){
  //   if (err) throw err
  //   else {
  //     if(row!=[])
  //     global.kk=global.kk.concat(row);
  //     console.log("最终：",global.kk)
  //     res.send(global.kk)
  //   }
  // });

/*

  db.all(y1,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(y2,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(y3,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(y4,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(y5,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });



  db.all(w1,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(w2,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(w3,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(w4,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(w5,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });


  db.all(t1,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(t2,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(t3,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(t4,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(t5,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });


  db.all(u1,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(u2,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(u3,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(u4,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
    }
  });
  db.all(u5,function(err,row){
    if (err) throw err
    else {
      console.log(row)
      if(row!=[])
      global.kk=global.kk.concat(row);
      console.log("最终：",global.kk)
      res.send(global.kk)
    }
  });
*/
/*
  db.all(sqlStr,function(err,row){
    if (err) throw err
    else {
        //console.log(row)
        if(row.length!=0)
        {
          console.log("row:",row)
          res.send(row);
        }else{
            res.send('-1');
        }  
    }
  });*/
  db.close();
});

module.exports = router;
